package com.sec.samsung.android.app.baymax.view.activity;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.ScaleAnimation;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sec.samsung.android.app.baymax.R;
import com.sec.samsung.android.app.baymax.utils.BaymaxConstant;
import com.sec.samsung.android.app.baymax.utils.SharePrefUtils;

public class WelcomeActivity extends Activity {

    private final int REQUEST_CODE_PRIVACY = 200;
    private RelativeLayout mCenterLayout;
    private LinearLayout mBottomLayout;
    private RelativeLayout mNextBtnLayout;
    private RelativeLayout mBackgroundLayout;
    private Animation mContentUpAnime;
    private Animation mFadeInAnime;
    private Animation mFadeOutAnime;
    private boolean isNext = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!SharePrefUtils.getBooleanValue(this, BaymaxConstant.KEY_PASSED_FIRST_TIME_INITIALIZE)) {
            setContentView(R.layout.activity_welcome);
            initLayout();
        } else {
            Intent intent = new Intent(this, ProfileActivity.class);
            startActivity(intent);
            finish();
        }
    }

    private void initLayout() {
        mBottomLayout = (LinearLayout) findViewById(R.id.welcome_intro_bottom_layout);
        mBottomLayout.setVisibility(View.GONE);
        mBackgroundLayout = (RelativeLayout) findViewById(R.id.welcome_background);
        mCenterLayout = (RelativeLayout) findViewById(R.id.welcome_intro_center_layout);
        mNextBtnLayout = (RelativeLayout) findViewById(R.id.intro_next_layout);
        mContentUpAnime = AnimationUtils.loadAnimation(this, R.anim.welcome_initial_content_up);
        mFadeInAnime = AnimationUtils.loadAnimation(this, R.anim.welcome_image_fade_in);
        if (mFadeInAnime != null) {
            mFadeInAnime.setAnimationListener(fadeInAniListener);
        }

        mFadeOutAnime = AnimationUtils.loadAnimation(this, R.anim.welcome_intro_fade_out);
        if (mFadeOutAnime != null) {
            mFadeOutAnime.setAnimationListener(fadeOutAniListener);
        }

        String welcomeDescription = getResources().getString(R.string.welcome_description);
        TextView textView = (TextView) findViewById(R.id.welcome_desc_text);
        String appName = getResources().getString(R.string.app_name).replace(" ", "\u00a0");
        String result = String.format(welcomeDescription, appName, appName);
        textView.setText(result);

        mNextBtnLayout.setOnClickListener(startClickListener);

        mBottomLayout.setVisibility(View.VISIBLE);
        mBottomLayout.setBackgroundColor(0xcc0089c4);
        mCenterLayout.setVisibility(View.VISIBLE);

        if (mFadeInAnime != null) {
            mCenterLayout.startAnimation(mFadeInAnime);
        }

        mBottomLayout.startAnimation(mContentUpAnime);

        MediaPlayer mPlayer = MediaPlayer.create(WelcomeActivity.this, R.raw.baymax_sound_track);
        try {
            mPlayer.start();
        } catch (Exception e) {
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

        // if (mBackgroundLayout == null)
        initLayout();

        mBackgroundLayout.setVisibility(View.GONE);
        mNextBtnLayout.requestFocus();
    }

    @Override
    public void onBackPressed() {
        setResult(RESULT_CANCELED);
        super.onBackPressed();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == REQUEST_CODE_PRIVACY) {
            if (resultCode == RESULT_OK) {
                setResult(RESULT_OK);
                Bundle bundle = getIntent().getBundleExtra("launch");
                if (bundle != null) {
                    String packageName = bundle.getString("packageName");
                    String activityName = bundle.getString("activityName");
                    String REMINDER_TEXT = bundle.getString("REMINDER_TEXT");
                    String action = bundle.getString("action");
                    Intent intent = new Intent();
                    intent.setAction(action);
                    intent.putExtra("REMINDER_TEXT", REMINDER_TEXT);
                    ComponentName componentName = new ComponentName(packageName, activityName);
                    intent.setComponent(componentName);
                    startActivity(intent);
                    finish();
                    return;
                }
                Intent intent = new Intent(this, ProfileActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(intent);
                finish();
            }
        }
    }

    private AnimationListener fadeInAniListener = new AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            mNextBtnLayout.requestFocus();
        }
    };

    private AnimationListener fadeOutAniListener = new AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            Handler handler = new Handler();
            handler.postDelayed(run, 1000);

            mCenterLayout.setVisibility(View.GONE);
            mBottomLayout.setVisibility(View.GONE);
            Intent intent = new Intent(getApplicationContext(), TermsOfUseActivity.class);
            startActivityForResult(intent, REQUEST_CODE_PRIVACY);
        }
    };

    Runnable run = new Runnable() {
        @Override
        public void run() {
            isNext = false;
            mNextBtnLayout.setSoundEffectsEnabled(true);
        }
    };

    View.OnClickListener startClickListener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (!isNext) {
                isNext = true;
                mNextBtnLayout.setSoundEffectsEnabled(false);

                if (mFadeOutAnime != null) {
                    mCenterLayout.startAnimation(mFadeOutAnime);
                    mBottomLayout.startAnimation(mFadeOutAnime);
                }
                mBottomLayout.setBackground(null);
                mBackgroundLayout.setVisibility(View.VISIBLE);

                AnimationSet animSet = new AnimationSet(true);
                animSet.setInterpolator(new AccelerateInterpolator(1.8f));
                Animation aAlpha = new AlphaAnimation(0.8f, 1.0f);
                aAlpha.setDuration(1000);
                animSet.addAnimation(aAlpha);
                int height = mBottomLayout.getHeight();
                int screenHeight = getWindowManager().getDefaultDisplay().getHeight();
                float fromYScale = (float) height / (float) screenHeight;
                ScaleAnimation aScale = new ScaleAnimation(1.0f, 1.0f, fromYScale, 1.0f, 0, screenHeight);
                aScale.setDuration(1000);
                animSet.addAnimation(aScale);
                mBackgroundLayout.startAnimation(animSet);
            }
        }
    };
}
